package com.ruike.alisurface;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;
import android.support.multidex.MultiDex;
import android.text.TextUtils;

import com.github.moduth.blockcanary.BlockCanary;
import com.ruike.alisurface.Serials.ICCardUtils;
import com.ruike.alisurface.Serials.Ttys3Utils;
import com.ruike.alisurface.Serials.Ttys1Utils;
import com.ruike.alisurface.Serials.Ttys2Utils;
import com.ruike.alisurface.services.NetWorkListenerService;
import com.ruike.alisurface.utils.CrashHandler;
import com.ruike.alisurface.utils.DeviceUtils;
import com.ruike.alisurface.utils.FinalDbUtils;
import com.ruike.alisurface.utils.Languages.LanguageUtil;
import com.ruike.alisurface.utils.ShareKey;
import com.ruike.lib_serialport.utils.SerialPortTtys1CallBack;
import com.ruike.lib_serialport.utils.SerialPortTtys2CallBack;
import com.ruike.lib_serialport.utils.SerialPortTtys3CallBack;
import com.tencent.bugly.crashreport.CrashReport;
import com.voodoo.lib_frame.FrameApplication;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.ShareUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import static com.ruike.alisurface.Constant.isOpenCardOperation;

/**
 * Author: voodoo
 * CreateDate: 2020-03-25 025 下午 02:55
 * Description:
 */
public class MyApplication extends FrameApplication {

    private static Context context;
    public static FinalDbUtils finalDbUtils;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onCreate() {
        super.onCreate();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());
//        SkinManager.init(this);
        context = FrameApplication.getFrameContext();
        finalDbUtils = FinalDbUtils.getInstance(this);
        ShareUtils.init(this);
        /**
         * 对于7.0以下，需要在Application创建的时候进行语言切换
         */
        String language = ShareUtils.getInstance().getString(ShareKey.LANGUAGE, "");
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            LanguageUtil.changeAppLanguage(this, language);
        }
        if (isOpenCardOperation) {
            openSerialPort1(); // 打开串口1
        }
        startService(new Intent(this, NetWorkListenerService.class));
//        openSerialPort2(); // 打开串口2
        openSerialPort3(); // 打开串口3

        // 在主进程初始化调用哈
        BlockCanary.install(this, new AppBlockCanaryContext()).start();
//        sRefWatcher = LeakCanary.install(this);
        // 获取当前包名
        String packageName = context.getPackageName();
// 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
// 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
// 初始化Bugly
        CrashReport.initCrashReport(this, "aacf3b1ffb", true);
        CrashReport.setUserId(this, DeviceUtils.getDeviceSn());
    }


    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 打开串口1
     */
    private void openSerialPort1() {
        SerialPortTtys1CallBack.setCallBack(Ttys1Utils.getInstance());
        Ttys1Utils.getInstance().OpenPort();
        SystemClock.sleep(500);
        ICCardUtils.initListener(); // 初始化读卡器的串口回调监听
        ICCardUtils.readWriteMode(false); // 设置被动读写模式
    }

    /**
     * 打开串口2
     */
    private void openSerialPort2() {
        SerialPortTtys2CallBack.setCallBack(Ttys2Utils.getInstance());
        Ttys2Utils.getInstance().OpenPort();
    }

    /**
     * 打开串口3
     */
    private void openSerialPort3() {
        L.i("打开串口");
        SerialPortTtys3CallBack.setCallBack(Ttys3Utils.getInstance());
        Ttys3Utils.getInstance().OpenPort();

    }

    /**
     * 获取全局Context
     */
    public static Context getAppContext() {
        return context;
    }

}
